<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>登陆</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/5.1.1/css/bootstrap.min.css">
    <script src="https://cdn.staticfile.org/twitter-bootstrap/5.1.1/js/bootstrap.bundle.min.js"></script>
    <script src="https://unpkg.com/vue@2.7.16/dist/vue.min.js"></script>
    <script src="/static/js/axios.js"></script>
    <script src="/static/js/server.js"></script>
    <style>
        div.container {
            margin-top: 50px;
        }

        form {
            margin: 20px;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="container col-4">
            <h1 class="text-center">登录</h1>
            <div>
                <div class="mb-3">
                    <label for="inputUserName">用户名</label>
                    <input type="text" class="form-control" v-model="userInfo.account">
                </div>
                <div class="mb-3">
                    <label for="inputPassword">密码</label>
                    <input type="password" class="form-control" v-model="userInfo.password">
                </div>
                <div class="mb-3 d-flex flex-row justify-content-between align-items-end">
                    <div>
                        <label>验证码</label>
                        <input type="code" class="form-control" v-model="userInfo.code">
                    </div>
                    <img style="height: 40px;" @click="getVerifyCode" :src="verifyCode" alt="验证码">
                </div>
                <div class="d-flex flex-row justify-content-between">
                    <button class="btn btn-primary" @click="login">登录</button>
                    <a class="btn border" href="/app/view/register.html">注册</a>
                </div>
            </div>
            <div v-show="errorInfo.length > 0"
                class="position-fixed top-2 alert alert-danger alert-dismissible fade show">
                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                {{errorInfo}}
            </div>
        </div>
    </div>
</body>
<script>
    var vueObj = new Vue({
        el: "#app",
        data: {
            userInfo: { account: "test", password: "123456" },
            errorInfo: "",
            verifyCode: "",
        },
        created() {
            this.getVerifyCode()
        },
        methods: {
            login() {
                axios.post('http://127.0.0.1:8001/user/login', this.userInfo).then(res => {
                    console.log(res);
                    if (res.code == 200) {
                        window.location.href = '/app/view/index.html';
                    } else {
                        this.errorInfo = res.msg;
                    }
                })
            },
            getVerifyCode() {
                axios.post('http://127.0.0.1:8001/user/verifyCode').then(res => {
                    console.log(res);
                    if (res.code == 200) {
                        this.verifyCode = res.data.code_url;
                    } else {
                        this.errorInfo = res.msg;
                    }
                })
            },
        }
    })
</script>
<style>
body{
    filter: grayscale(1);
}
</style>
</html>